/******------ 用户信息帮助类---------********/
/******------ 此类为辅助类为了转换服务器返回的数据---------********/

/**
 * 二级菜单转换类
 * @param  {[type]} params 原二级菜单数据
 * @return {[type]}        转换后二级菜单数据
 */
export function getSiderMenus(params) {
  return params.map((n) => {
    let obj = {};
    let chid = getSiderMenus(n.children);
    if (chid.length > 0) {
      obj.children = chid;
    }
    return { ...n.root, ...obj };
  });
}

/**
 * 获取二级菜单地一个显示
 * @param  {[type]} array   [description]
 * @param  {[type]} menuKey [description]
 * @return {[type]}         [description]
 */
export function getMeunFistKey(array, menuKey) {
  if (!array.length) {
    return { dsplayMenuKey: '', breadPath: '' };
  }
  if (menuKey) {
    return {
      dsplayMenuKey: menuKey,
      menuCode: getMeunCode(array, menuKey),
      breadPath: getMeunKey(array, menuKey),
    };
  }
  let keyVal = '';
  let fistCode = '';
  let breadPath = '';
  let fistItem = array?.[0];
  if (!fistItem) {
    return keyVal;
  }
  if (fistItem.children && fistItem.children.length > 0) {
    let obj = getMeunFistKey(fistItem.children);
    if (breadPath.length > 0) {
      breadPath += '/';
    }
    keyVal = obj.dsplayMenuKey;
    fistCode = obj.menuCode;
    breadPath += obj.breadPath;
  }
  if (breadPath.length > 0) {
    breadPath += '/';
  }
  breadPath += fistItem.menuName;

  if (keyVal === '') {
    fistCode = fistItem.menuCode;
    keyVal = fistItem.component;
  }
  breadPath = breadPath.split('/').reverse().join('/');
  return { dsplayMenuKey: keyVal, menuCode: fistCode, breadPath: breadPath };
}

// t_data_source 数据源 ,t_station 节算单元 ,t_region 区域, t_decompose_strategy 策略
export const powersDataType = {
  settlement: 'sell_electricity_company',
  source: 't_data_source',
  station: 't_station',
  region: 't_region',
  dept: 't_dept',
  strategy: 't_decompose_strategy',
  province_region: 't_province_region',
};

/**
 * 获取菜单的code值
 * @param  {[type]} array  [description]
 * @param  {[type]} keyVal [description]
 * @return {[type]}        [description]
 */

export function getMeunCode(array, keyVal) {
  let codeObj = { codeStr: '' };
  array.forEach((item) => {
    if (item.component == keyVal) {
      codeObj.codeStr = item.menuCode;
    } else {
      if (item.children) {
        findMenucode(item.children, keyVal, codeObj);
      }
    }
  });
  return codeObj.codeStr;
}

function findMenucode(array, keyVal, codeObj) {
  array.forEach((item) => {
    if (item.component == keyVal) {
      codeObj.codeStr = item.menuCode;
    } else {
      if (item.children) {
        findMenucode(item.children, keyVal, codeObj);
      }
    }
  });
}

const getOneMeunKey = function (array, keyVal) {
  let arr = array.filter((item) => item.component === keyVal);
  if (arr.length > 0) {
    const item = arr?.[0];
    return { array: item.children, breadName: item.menuName };
  }
  return undefined;
};

/**
 * 获取菜单的key值
 * @param  {[type]} array  [description]
 * @param  {[type]} keyVal [description]
 * @return {[type]}        [description]
 */
export function getMeunKey(array, keyVal) {
  let arr = keyVal.split('/');
  let breadPath = '';
  let temp = array;
  let tempKey = '';
  arr.forEach((item) => {
    tempKey += item;
    let x = getOneMeunKey(temp, tempKey);
    if (x !== undefined) {
      if (breadPath.length > 0) {
        breadPath += '/';
      }
      if (tempKey.length > 0) {
        tempKey += '/';
      }
      breadPath += x.breadName;
      if (x.array !== null) {
        temp = x.array;
      }
    }
  });
  return breadPath;
}
